﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;

/// <summary>

/// </summary>
public class DBaccess
{
    private static string dbconnection = ConfigurationManager.ConnectionStrings["MobileResourceConnectionString"].ConnectionString;
	
    public DBaccess()
    {

    }

    public bool Exists(String SqlString)
    {
        int result = ExecSql(SqlString);
        if (result == 0)
            return false;
        else
            return true;
    }
    
    public int ExecSql(String SqlString)
	{
        SqlConnection connection = new SqlConnection(dbconnection);
        SqlCommand cmd = new SqlCommand(SqlString,connection);
        try{
            connection.Open();
            int rows = cmd.ExecuteNonQuery();
            return rows;
        }
        catch(System.Data.SqlClient.SqlException E)
        {
            connection.Close();
            throw new Exception(E.Message);
        }

	}

    public void ExecTranscation(String[] SqlString)
    {
        SqlConnection connection = new SqlConnection(dbconnection);
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connection;
        SqlTransaction st = connection.BeginTransaction();
        cmd.Transaction = st;
        String sqlText;
        try
        {
            for (int i = 0; i < SqlString.Length; i++)
                if ((sqlText = SqlString[i]).Length > 1)
                {
                    cmd.CommandText = sqlText;
                    cmd.ExecuteNonQuery();
                }
            st.Commit();
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            st.Rollback();
            throw new Exception(E.Message);
        }
    }

    public SqlDataReader GetReader(String SqlString)
    {
        SqlConnection connection = new SqlConnection(dbconnection);
        SqlCommand cmd = new SqlCommand(SqlString, connection);
        try
        {
            connection.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            return sdr;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            connection.Close();
            throw new Exception(E.Message);
        }
    }

    public DataSet GetDataSet(String SqlString)
    {
        SqlConnection connection = new SqlConnection(dbconnection);
        SqlDataAdapter sda = new SqlDataAdapter(SqlString, connection);
        try
        {
            connection.Open();
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            connection.Close();
            throw new Exception(E.Message);
        }
        finally
        {
            connection.Close();
        }
    }
}